/*
 *   Copyright 2021 zzh
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */

package com.aduib.boot.web.context;

import com.aduib.boot.web.exception.ReqAndRepsNotFoundException;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @description: RequestHelper
 * @author: zzh
 * @date: 2021/9/6 16:15
 */
public final class RequestHelper {

  /**
   * 获取当前请求对象
   * @return
   */
  public static HttpServletRequest getServletRequest(){
    HttpServletRequest request=null;
    try{
      request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    }catch(Exception e){
      throw new ReqAndRepsNotFoundException("HttpServletRequest object not found");
    }
    return request;
  }

  /**
   * 获取当前响应对象
   * @return
   */
  public static HttpServletResponse getServletResponse(){
    HttpServletResponse response;
    try{
      response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
    }catch(Exception e){
      throw new ReqAndRepsNotFoundException("HttpServletResponse object not found");
    }
    return response;
  }
}
